<template>
  <div class="project_container">
    <nav class="project_nav">
      <img :src="navBg" alt="" class="project_nav_bg">
      <div class="project_nav_content" :class="isFixed?'bg1':''">
        <div class="project_nav_logo">
          <span class="shadow">vite-design</span>
        </div>
        <div class="project_nav-right">
          <el-dropdown trigger="click">
            <div class="project_nav-right_user">
                        <span>
                          <img src="http://files.pengxiaotian.com/avatar.jpeg?imageView2/1/w/80/h/80" alt="">
                        </span>
              <span>美人骨</span>
              <span class="iconfont icon-xiala1"></span>
              <div class="projecy_nav-right_user_icon"></div>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item><span class="iconfont icon-tuichu"></span>退出</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </nav>
    <div class="project_nav-main"
         ref="navMainRef"
         :class="isFixed?'bg1':''">
      <canvas id="nav-canvas" style="position: absolute; z-index: -1; left: 0;"></canvas>
      <span v-for="nav in navs" :key="nav.id" class="nav-span">
        <a class="nav-link"
           :class="{ 'nav-active': activeNav === nav.id }"
           @click="toggleNav(nav)">
          <i :class="`iconfont ${nav.icon} nav-icon`"></i>{{ nav.name }}
        </a>
      </span>
    </div>
    <div class="project-main">
      <div class="project-content">
        <router-view/>
      </div>
    </div>
  </div>
</template>

<script>
import { defineComponent, onMounted, onUnmounted, ref } from "vue";
import { debounce } from "../../utils/utils";
import { NavCanvas } from "./NavCanvas";
import { useRoute, useRouter } from "vue-router";
import navBg from "../../assets/png/nav-img.png";
import { useStore } from "vuex";

export default defineComponent({
  name: "project",
  setup() {
    let nc = null;
    const route = useRoute();
    const store = useStore();
    const router = useRouter();
    const isFixed = ref(false);
    const navMainRef = ref(null);
    const navs = ref([
      { id: 0, key: "project", name: "我的可视化", icon: "icon-MyProject", path: "/project/list" },
      { id: 1, key: "data", name: "我的数据", icon: "icon-MyData", path: "/data/source" },
      { id: 2, key: "com", name: "我的组件", icon: "icon-MyCom", path: "/com" },
      { id: 3, key: "case", name: "教程", icon: "icon-MyCase", path: "/case" }
    ]);
    const activeNav = ref(0);
    const toggleNav = (nav) => {
      if (nc) {
        activeNav.value = nav.id;
        nc.toggle(nav.id);
        router.replace({ path: nav.path });
      }
    };
    const debNavResize = debounce(() => {
      if (nc) {
        nc.resize();
      }
    }, 500);
    const scroll = () => {
      isFixed.value = navMainRef.value.offsetTop > 246;
    };
    onMounted(() => {
      const nav = navs.value.find(m => {
        let path = route.path;
        return path.includes(m.key) && path.slice(1,).startsWith(m.key);
      });
      activeNav.value = nav ? nav.id : 0;
      nc = new NavCanvas("nav-canvas", ".project_nav-main .nav-span", activeNav.value);
      window.addEventListener("resize", debNavResize);
      window.addEventListener("scroll", scroll);
    });
    onUnmounted(() => {
      window.removeEventListener("resize", debNavResize);
      window.removeEventListener("scroll", scroll);
    });
    return {
      navs,
      toggleNav,
      isFixed,
      navMainRef,
      activeNav,
      navBg,
      handleSelect(key) {
        switch (key) {
          case "logout":
            store.dispatch("user/FedLogOut").then(() => {
              router.push({ path: "/login" });
            }).catch(() => {
              router.push({ path: "/login" });
            });
            break;
          default:
            break;
        }
      }
    };
  }
});
</script>

<style lang="less" scoped>
@import "./../../style/page/project.less";
</style>
